﻿@inject DialogService DialogService
@inject IProductService ProductService
@inject IProductUnitService ProductUnitService

<RadzenStack>
    <RadzenTemplateForm EditContext="editContext" TItem="ProductDto" Data="@Model" Submit=@OnSubmit>
        <RadzenFieldset Text="商品信息">
            <RadzenStack Gap="1rem">
                <RadzenStack class="rz-p-0" 
                             Orientation="Orientation.Vertical" 
                             AlignItems="AlignItems.Normal"
                             JustifyContent="JustifyContent.SpaceBetween">
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="名称" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox Name="Name" @bind-Value="@Model!.Name" />
                                        <RadzenRequiredValidator Component="Name" Text="名称不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">
                                            * 必填
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                    
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="价格" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenNumeric @bind-Value="@Model!.Price" Name="Price"/>
                                        <RadzenRequiredValidator Component="Price" Text="单价不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">
                                            * 必填
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="代码" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@Model!.Code" Name="Code"/>
                                        <RadzenRequiredValidator Component="Code" Text="代码不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">
                                            * 必填
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn>
                                <RadzenStack Style="display:flex" Orientation="Orientation.Horizontal">
                                    <RadzenLabel Style="min-width: fit-content; align-items: center" Text="单位" Component="DropDownBindValue" />
                                <RadzenDropDown @bind-Value=@Model!.Unit Data=@unitNames Style="width: 100%;" Name="DropDownBindValue" />
                                </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                    
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="备注" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@Model!.Remark" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                </RadzenStack>
                <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
                    <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存" ButtonType="ButtonType.Submit" />
                </RadzenStack>
            </RadzenStack>
        </RadzenFieldset>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    [Parameter] public ProductDto? PProduct { get; set; }
    [Parameter] public long PProductTypeId { get; set; }

    private ProductDto? Model { get; set; }
    EditContext? editContext;

    private string? unitName { get; set; }
    IEnumerable<string> unitNames = default!;

    void OnSubmit()
    {
        try
        {
            ProductService.Save(Model!);
            DialogService.Close(Model!);
        }
        catch (Exception e)
        {
            DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    private void OnCancel()
    {
        DialogService.Close(null);
    }

    protected override async Task OnInitializedAsync()
    {
        Model = PProduct!;
        editContext = new EditContext(Model);

        var unitList = await ProductUnitService.Search(new BaseInput());
        unitNames = unitList.Select(s => s.Name);
        //unitName = unitNames.FirstOrDefault();
        Model.Unit = Model!.Unit ?? unitNames.First();

       StateHasChanged();
    }
}

